/**
 * Created by Administrator on 2019/5/23 0023.
 */
import CZML_GENERALTOOLS_CLASS from "../main/CZML_GENERALTOOLS_CLASS.js"

class CZML_Line {
  constructor(
    viewer,
    id,
    name,
    DataStartTimeStartPos,
    DataStartTimeEndPos,
    StartPos,
    StartPosType,
    EndPos,
    EndPosType,
    LineWidth,
    LineColor,
    LineStyle,
    MarkerVisible,
    ReferenceFrame
  ) {
    this._viewer = viewer
    this.id = id
    this.name = name
    this.DataStartTimeStartPos = DataStartTimeStartPos
    this.DataStartTimeEndPos = DataStartTimeEndPos
    this.StartPos = StartPos
    this.StartPosType = StartPosType
    this.EndPos = EndPos
    this.EndPosType = EndPosType
    this.LineWidth = LineWidth
    this.LineColor = LineColor
    this.LineStyle = LineStyle
    this.MarkerVisible = MarkerVisible
    this.ReferenceFrame = ReferenceFrame
    this.AddLine()
  }

  AddLine() {
    const posAid = this.id + "-to-posA"
    const posBid = this.id + "-to-posB"

    // UTCG时间格式转换
    const sDataStartTimeStartPos = CZML_GENERALTOOLS_CLASS.Utcg2ISO8601TimeTransformation(this.DataStartTimeStartPos)
    const sDataStartTimeEndPos = CZML_GENERALTOOLS_CLASS.Utcg2ISO8601TimeTransformation(this.DataStartTimeEndPos)

    const posA = {
      id: posAid,
      name: this.name,
      availability: CZML_GENERALTOOLS_CLASS.HEU_ScenarioTime,
      show: true,
      label: {
        fillColor: {
          rgba: this.LineColor
        },
        font: "11pt Lucida Console",
        horizontalOrigin: "CENTER",
        pixelOffset: {
          cartesian2: [30, 10]
        },
        show: false,
        style: "FILL_AND_OUTLINE",
        text: name,
        verticalOrigin: "CENTER"
      },
      position: {
        interpolationAlgorithm: "LAGRANGE",
        interpolationDegree: 5,
        referenceFrame: this.ReferenceFrame,
        epoch: sDataStartTimeStartPos,
        cartesian: this.StartPos
      }
    }
    if (this.StartPosType === 1) {
      const StartPos = this.StartPos
      const FindObjectNode = CZML_GENERALTOOLS_CLASS.CzmlList.find(function (node) {
        return node.data().id === StartPos
      })

      const DatPosRef = FindObjectNode.data().position
      posA.position = DatPosRef
    }

    const posB = {
      id: posBid,
      name: this.name,
      availability: CZML_GENERALTOOLS_CLASS.HEU_ScenarioTime,
      show: true,
      label: {
        fillColor: {
          rgba: this.LineColor
        },
        font: "11pt Lucida Console",
        horizontalOrigin: "CENTER",
        pixelOffset: {
          cartesian2: [30, 10]
        },
        show: this.MarkerVisible,
        style: "FILL_AND_OUTLINE",
        text: name,
        verticalOrigin: "CENTER"
      },
      position: {
        interpolationAlgorithm: "LAGRANGE",
        interpolationDegree: 5,
        referenceFrame: this.ReferenceFrame,
        epoch: sDataStartTimeEndPos,
        cartesian: this.EndPos
      }
    }
    if (this.EndPosType === 1) {
      const FindObjectNode = CZML_GENERALTOOLS_CLASS.CzmlList.find(function (node) {
        return node.data().id === this.EndPos
      })

      const DatPosRef = FindObjectNode.data().position
      posB.position = DatPosRef
    }

    // 插入该数据对象
    const ret1 = CZML_GENERALTOOLS_CLASS.InsertModelObjectInformation(posAid, posA, true)
    const ret2 = CZML_GENERALTOOLS_CLASS.InsertModelObjectInformation(posBid, posB, true)

    const PosARef = posAid + "#position"
    const PosBRef = posBid + "#position"

    const Line = {
      id: this.id,
      name: this.name,
      availability: CZML_GENERALTOOLS_CLASS.HEU_ScenarioTime,
      polyline: {
        show: [
          {
            interval: CZML_GENERALTOOLS_CLASS.HEU_ScenarioTime,
            boolean: true
          }
        ],
        width: this.LineWidth,
        material: {
          solidColor: {
            color: {
              rgba: this.LineColor
            }
          }
        },
        followSurface: false,
        positions: {
          references: [PosARef, PosBRef]
        }
      }
    }
    if (this.LineStyle === 1) {
      Line.polyline.material = {
        polylineDash: {
          color: {
            rgba: this.LineColor
          }
        }
      }
    }

    // 箭头
    if (this.LineStyle === 2) {
      Line.polyline.material = {
        polylineArrow: {
          color: {
            rgba: this.LineColor
          }
        }
      }
    }

    // 插入该数据对象
    const ret3 = CZML_GENERALTOOLS_CLASS.InsertModelObjectInformation(this.id, Line, true)

    // 上图
    CZML_GENERALTOOLS_CLASS.Upper(this._viewer)
    return ret1 && ret2 && ret3
  }
}

export default CZML_Line
